package com.cstz.ydshop.common.utils;

import java.math.BigDecimal;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class MoneyUtil {
	
	private final static Log logger = LogFactory
			.getLog(MoneyUtil.class);
	
	public static String toYuanNumber(Long price){
		if(price == null) return "0.00";
		
		BigDecimal val = new BigDecimal(price);
		val = val.divide(new BigDecimal(100));
		
		val=val.setScale(2, BigDecimal.ROUND_HALF_UP);
		
		return val.toString();
	}
	
	public static String toYuanNumber(Integer price){
		if(price == null) return "0.00";
		
		BigDecimal val = new BigDecimal(price);
		val = val.divide(new BigDecimal(100));
		
		val=val.setScale(2, BigDecimal.ROUND_HALF_UP);
		
		return val.toString();
	}
	
	public static Float toYuanNumberFloat(Long price){
		if(price == null) return 0.00f;
		
		BigDecimal val = new BigDecimal(price);
		val = val.divide(new BigDecimal(100));
		
		val=val.setScale(2, BigDecimal.ROUND_HALF_UP);
		
		return val.floatValue();
	}
	
	public static Long toFenNumber(Long price){
		return price * 100;
	}
	
	/**
     * str转换为double再转换为分－工具方法
     * 
     * @author zhengshutian
     * 
     */
	public static Integer str2Double2Fen(String price){
		BigDecimal val = new BigDecimal(price);
		
		//保留两位小数，向上取四舍五入   20.4587 -->  20.46
		val=val.setScale(2, BigDecimal.ROUND_HALF_UP);
		
		//20.46 -->  2046
		val = val.multiply(new BigDecimal(100));
		
		return val.intValue();
	}
	
	/**
     * str转换为double再转换为分－工具方法
     * 
     * @author zhengshutian
     * 
     */
	public static String date2Str(Date time){
		
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		
		String date="";
		try{
			date	 = df.format(time);
		}
		catch(Exception e){
			e.printStackTrace();
		}
		
		return date;
	}
	/**
     * double类型转String
     * 
     * @author sunjinping
     * 
     */
    public static String turn(double data){
    	
    	BigDecimal bd = toSignificantFigures(data, 4); 
        return bd.multiply(new BigDecimal(100)).toString()+"%";
        
    }
   

    public static BigDecimal toSignificantFigures(double data, int significantFigures){
    	String s = String.format("%."+significantFigures+"G", BigDecimal.valueOf(data));
        BigDecimal result = new BigDecimal(s);
        return result;
    }
    
	public static void main(String[] args) {
		System.out.println(str2Double2Fen("2.5"));
		System.out.println(toYuanNumber(20l));
	}
}
